//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "PricingEngine.h"
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::CPricingEngine::CPricingEngine (boost::shared_ptr<QuantLib::PricingEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPricingEngine = NULL;
#endif
	_ppPricingEngine = &childNative;
    
}
Cephei::QL::CPricingEngine::CPricingEngine (QuantLib::PricingEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phPricingEngine = NULL;
#endif
	_ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (&childNative);
    
    _PricingEngineOwner = owner;
    
}

Cephei::QL::CPricingEngine::CPricingEngine (CPricingEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPricingEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (copy->GetShared());
        
    }
}
Cephei::QL::CPricingEngine::CPricingEngine (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPricingEngine = NULL;
#endif
	if (!t->IsSubclassOf(CPricingEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CPricingEngine::CPricingEngine (QuantLib::Handle<QuantLib::PricingEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPricingEngine = &childNative;
	_ppPricingEngine = &static_cast<boost::shared_ptr<QuantLib::PricingEngine>>(childNative.currentLink());
    
    _PricingEngineOwner = owner;
}
Cephei::QL::CPricingEngine::CPricingEngine (QuantLib::Handle<QuantLib::PricingEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPricingEngine = &childNative;
	_ppPricingEngine = &static_cast<boost::shared_ptr<QuantLib::PricingEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CPricingEngine::CPricingEngine (QuantLib::PricingEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPricingEngine = NULL;
#endif
	_ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (new QuantLib::PricingEngine (childNative));
    
}
#endif

Cephei::QL::CPricingEngine::~CPricingEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppPricingEngine != NULL)
    {
	    delete _ppPricingEngine;
        _ppPricingEngine = NULL;
    }
}
Cephei::QL::CPricingEngine::!CPricingEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppPricingEngine != NULL)
    {
	    delete _ppPricingEngine;
    }
}
QuantLib::PricingEngine& Cephei::QL::CPricingEngine::GetReference ()
{
    if (_ppPricingEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppPricingEngine;
}
boost::shared_ptr<QuantLib::PricingEngine>& Cephei::QL::CPricingEngine::GetShared ()
{
    if (_ppPricingEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppPricingEngine;
}
QuantLib::PricingEngine* Cephei::QL::CPricingEngine::GetPointer ()
{
    if (_ppPricingEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppPricingEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PricingEngine>& Cephei::QL::CPricingEngine::GetHandle ()
{
	if (_phPricingEngine == NULL)
	{
		_phPricingEngine = new Handle<QuantLib::PricingEngine> (*_ppPricingEngine);
	}
	return *_phPricingEngine;
}
#endif
bool Cephei::QL::CPricingEngine::HasNative () 
{
	return (_ppPricingEngine != NULL);
}

Cephei::QL::IPricingEngine^ Cephei::QL::CPricingEngine::Calculate::get ()
{
    try
    {
    	(*_ppPricingEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IPricingEngine^ Cephei::QL::CPricingEngine::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppPricingEngine)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

